﻿/// <reference path="JQuery.js" />

var Facebook = {
    Types: {
        Session: {
            UId: "",
            UserName: "",
            AccessToken: "",
            TokenSecret: "",
            Name: "",
            ProfileUrl: "",
            PicUrl: "",
            PicSquareUrl: "",
            Gender: "",
            DateBirthDay: ""
        },
        ImageType: {
            Square: "square",
            Normal: "normal"
        },
        DisplayType: {
            Touch: "touch",
            Page: "page"
        }
    }
};

Facebook.Session = Facebook.Types.Session;

(function () {
    var obj = this;

    function Initialize() {
        FB.init({ appId: "195265793830429", status: true, cookie: true, xfbml: false });
        Facebook.Session = null;
    };

    function SetSession(response, callback) {
        if (response.authResponse) {
            Facebook.Session = Facebook.Types.Session;
            Facebook.Session.UId = response.authResponse.userID;
            Facebook.Session.AccessToken = response.authResponse.accessToken;
            Facebook.Session.TokenSecret = response.authResponse.signedRequest;
            FB.api(
                {
                    method: 'fql.query',
                    query: 'SELECT name, username, profile_url, pic, pic_square, sex, birthday_date FROM user WHERE uid = me()'
                },
                function (responseQ) {
                    Facebook.Session.Name = responseQ[0].name;
                    Facebook.Session.ProfileUrl = responseQ[0].profile_url;
                    Facebook.Session.PicUrl = responseQ[0].pic;
                    Facebook.Session.PicSquareUrl = responseQ[0].pic_square;
                    Facebook.Session.UserName = responseQ[0].username;
                    Facebook.Session.Gender = responseQ[0].sex;
                    Facebook.Session.DateBirthDay = responseQ[0].birthday_date;
                    if ($.isFunction(callback))
                        callback();
                }
            );
        } else {
            Facebook.Session = null;
            if ($.isFunction(callback))
                callback();
        }
    };

    function GetSession(callback) {
        FB.getLoginStatus(function (response) {
            SetSession(response, callback);
        });
    };

    function Login2(callback) {
        FB.login(function (response) {
            SetSession(response, callback);
        }, { scope: "read_stream, publish_stream, offline_access, email, user_birthday, user_about_me, user_location, user_status, user_relationships, user_photos" });
    };

    function Login(typeDisplay) {
        var path = 'https://www.facebook.com/dialog/oauth?';
        var queryParams = ["client_id=195265793830429","scope=read_stream,publish_stream,offline_access,email,user_birthday,user_about_me,user_location,user_status,user_relationships,user_photos", "redirect_uri=" + window.location.origin, "display=" + typeDisplay];
        var query = queryParams.join('&');
        var url = path + query;
        window.location = url;
    };

    function GetImageUrl(uId, imageType) {
        return "http://graph.facebook.com/" + uId + "/picture?type=" + imageType;
    };

    $.extend(Facebook, {
        Initialize: Initialize,
        GetSession: GetSession,
        Login: Login,
        GetImageUrl: GetImageUrl
    });
})();